<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class svn extends admin {
	function __construct() {
		parent::__construct();
		$this->db = pc_base::load_model('svn_model');
	}
	
	//svn list
	function init () {
		$infos = array();
			
			
			$where = "";
			$page = $_GET['page'] ? $_GET['page'] : '1';
			$list = $this->db->listinfo($where, $order = 'id DESC', $page, $pagesize = 20);
			$pages = $this->db->pages;
			$show_dialog = true;
			$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m='.ROUTE_M.'&c='.ROUTE_C.'&a=add\', title:\'添加\', width:\'500\', height:\'300\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', '添加项目');
 			$big_menus = array(
 				array('javascript:window.top.art.dialog({id:\'config\',iframe:\'?m='.ROUTE_M.'&c='.ROUTE_C.'&a=config\', title:\'SVN设置\', width:\'800\', height:\'400\', lock:true}, function(){var d = window.top.art.dialog({id:\'config\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'config\'}).close()});void(0);', 'SVN设置'),
 			);
 			
			include $this->admin_tpl('svn/list');
	}
	function config() {
		$db = pc_base::load_model('svn_conf_model');
		$one = $db->get_one(array('vkey'=>'conf'));
		if(isset($_POST['dosubmit'])) {
			
			$info = $_POST['info'];
			
			if(!$one['id']){
				$db->insert(array('created'=>time(),'updated'=>time(),'vkey'=>'conf','data'=>json_encode($info)));
			}else{
				$one['data'] = json_encode($info);
				$one['updated'] = time();
				$db->update($info,array('vkey'=>'conf'));
			} 
			//结束
			showmessage(L('operation_success'), '', '', 'config');
		} else {
			
			$info = array();
			if($one['data'])$info = json_decode($one['data'],true);
			include $this->admin_tpl('svn/config');
		}
	}
	function add() {
		if(isset($_POST['dosubmit'])) {
			
			$info = $_POST['info'];
			
			$one = $this->db->get_one(array('name'=>$info['name']));
			if(!empty($one))showmessage('项目名称已存在');
			$one = $this->db->get_one(array('code'=>$info['code']));
			if(!empty($one))showmessage('项目目录已存在');
			$info['created'] = time();
			$this->db->insert($info);
			
			$svnServ = pc_base::load_sys_class('svnserv');
			$svnServ->create($info['code']);
			//结束
			showmessage(L('operation_success'), '', '', 'add');
		} else {
			
			
			include $this->admin_tpl('svn/add');
		}
	}
	function delete() {
		$_GET['id'] = intval($_GET['id']);
		$this->db->delete(array('id'=>$_GET['id']));
		showmessage(L('operation_success'));
	}
	
	function edit() {
		if(isset($_POST['dosubmit'])) {
			$id = intval($_POST['id']);
			
			$info = $_POST['info'];
			$one = $this->db->get_one("name='".$info['name']."' AND id<>".$id);
			if(!empty($one))showmessage('项目名称已存在');
			unset($info['code']);
			$this->db->update($info,array('id'=>$id));
			
			//结束语言文件修改
			showmessage(L('operation_success'), '', '', 'edit');
		} else {
			
			$id = intval($_GET['id']);
			$r = $this->db->get_one(array('id'=>$id));
			if($r) extract($r);
			
			include $this->admin_tpl('svn/edit');
		}
	}
	
	
}